package asynq

import (
	"errors"
	"net/http"

	"diff.wang/asynqsrv/clienthttp/internal/logic/asynq"
	"diff.wang/asynqsrv/clienthttp/internal/svc"
	"diff.wang/asynqsrv/clienthttp/internal/types"
	"diff.wang/asynqsrv/common/result"
	"diff.wang/asynqsrv/common/validate"
	"github.com/zeromicro/go-zero/rest/httpx"
)

func PhoneHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.PhoneReq
		if err := httpx.Parse(r, &req); err != nil {
			result.ParamErrorResult(r, w, err)
			return
		}
		if err := validate.Validate.StructCtx(r.Context(), req); err != nil {
			result.ParamErrorResult(r, w, errors.New(validate.Translate(err)))
			return
		}
		l := asynq.NewPhoneLogic(r.Context(), svcCtx)
		resp, err := l.Phone(&req)
		result.HttpResult(r, w, resp, err)
	}
}
